﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Specialized;

namespace SF.Base.Core {
	/// <summary>
	/// 元数据操作类
	/// </summary>
	public class MetaRules {

		#region 获取对象的元数据 public static StringDictionary GetMeta(string metatype,string object_id)

		/// <summary>
		/// 获取对象的元数据
		/// </summary>
		/// <param name="metatype">元数据类型</param>
		/// <param name="object_id">对象ID</param>
		/// <returns></returns>
		public static StringDictionary GetMeta(string metatype, string object_id) {

			StringDictionary sd = new StringDictionary();
			try {
				using (
					IDataReader dataReader = Sh.ExecuteReader(
						BaseSettings.Instance.ConnSF,
						CommandType.Text,
						@"
							select 
								metakey,
								metavalue 
							from dt_object_meta 
							where metatype = @metatype 
							and object_id = @object_id
						",
						Sh.GetParameter("@object_id", DbType.String, object_id),
						Sh.GetParameter("@metatype", DbType.String, metatype)
					)
				) {
					while (dataReader.Read()) {
						sd.Add(Sh.GetStr(dataReader, "metakey"), Sh.GetStr(dataReader, "metavalue"));
					}
				}
			}
			catch (System.Exception e) {
				Logger.Exception("用户元数据查询失败", e);
			}
			return sd;

		}

		#endregion

		#region 添加修改对象的元数据 public static int AddMeta(string metatype, string object_id, StringDictionary meta)

		/// <summary>
		/// 添加修改对象的元数据
		/// </summary>
		/// <param name="metatype">元数据类型</param>
		/// <param name="object_id">对象ID</param>
		/// <param name="metakey">元数据KEY</param>
		/// <param name="metavalue">元数据VALUE</param>
		/// <returns></returns>
		public static int AddMeta(string metatype, string object_id, string metakey, string metavalue) {
			StringDictionary userMeta = new StringDictionary();
			userMeta.Add(metakey, metavalue);
			return AddMeta(metatype, object_id, userMeta);
		}

		/// <summary>
		/// 添加修改对象的元数据
		/// </summary>
		/// <param name="metatype">元数据类型</param>
		/// <param name="object_id">对象ID</param>
		/// <param name="userMeta">元数据</param>
		/// <returns></returns>
		public static int AddMeta(string metatype, string object_id, StringDictionary meta) {

			if (meta.Count == 0) return -1;

			int index = 0;
			int count = 0;

			IDataParameter[] idas = new IDataParameter[meta.Count * 2 + 1];

			idas[count++] = Sh.GetParameter("@object_id", DbType.String, object_id);
			idas[count++] = Sh.GetParameter("@metatype", DbType.String, metatype);

			StringBuilder sb = new StringBuilder();
			foreach (string key in meta.Keys) {
				//如果EXISTS，则UPDATE，不存在INSERT
				sb.AppendFormat(" IF EXISTS ( select 1 from dt_object_meta where metatype=@metatype and metakey = @metakey{0} and  object_id = @object_id ) begin update dt_object_meta set metavalue = @metavalue{0} where metatype=@metatype and object_id = @object_id and metakey = @metakey{0} and status = 1 end else begin insert dt_object_meta (object_id,metatype,metakey,metavalue,status) values (@object_id,@metatype,@metakey{0},@metavalue{0},1) end ", index);

				idas[count++] = Sh.GetParameter(string.Format("@metakey{0}", index), DbType.String, key);
				idas[count++] = Sh.GetParameter(string.Format("@metavalue{0}", index), DbType.String, meta[key]);

				index++;
			}

			try {

				Sh.ExecuteNonQuery(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					sb.ToString(), idas
				);
			}
			catch (System.Exception e) {
				Logger.Exception("添加元数据失败", e);
				return -160213;
			}

			return 0;
		}

		#endregion

		#region 获取用户的元数据

		/// <summary>
		/// 获取用户的元数据
		/// </summary>
		/// <param name="userid">用户ID</param>
		/// <returns></returns>
		public static StringDictionary GetUserMeta(string userid) {
			return GetMeta("dt_users", userid);
		}
		public static int AddUserMeta(string userid,StringDictionary meta) {
			return AddMeta("dt_users", userid, meta);
		}
		public static int AddUserMeta(string userid, string key, string value) {
			StringDictionary userMeta = new StringDictionary();
			userMeta.Add(key, value);
			return AddMeta("dt_users", userid, userMeta);
		}

		#endregion
	}
}
